<template>
    <div class="message-board">
      <div class="header">
        <Header />
      </div>
      <div class="main">
        <div class="container">
          <!-- 寄语 -->
          <div class="message-intro">
            <h1 class="welcome-message">欢迎留言</h1>
            <p class="intro-text">
              分享你的想法、感受或建议，让我们一起交流与成长！<br />
              您的每一条留言，都是对博客的宝贵支持。
            </p>
          </div>
  
          <!-- 评论列表 -->
          <div class="message-section">
            <h2 class="widget-title">大家的留言</h2>
            <CommentList :comments="messages" v-if="messages && messages.length" @submit="submitMessage" />
            <el-empty v-else description="暂无留言，快来抢留言板哦！" />
          </div>
  
          <!-- 发布评论 -->
          <div class="comment-form-section">
            <h2 class="widget-title">发表留言</h2>
            <CommentForm @submit="submitMessage" />
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref, onMounted } from 'vue';
  import { ElMessage } from 'element-plus';
  import Header from '@/components/layout/Header.vue';
  import CommentList from '@/components/blog/CommentList.vue';
  import CommentForm from '@/components/blog/CommentForm.vue';
  import frontApi from '@/api/front';
  
  const messages = ref(null);
  
  const fetchMessages = async () => {
    try {
      const res = await frontApi.getMessages();
      console.log(res);
      messages.value = res.data;
    } catch (err) {
      ElMessage.error('获取留言失败');
      console.error('留言错误:', err);
    }
  };
  
  const submitMessage = async (data) => {
    try {
      await frontApi.createMessage(data);
      ElMessage.success('留言成功');
      fetchMessages();
    } catch (err) {
      ElMessage.error('留言失败');
      console.error('留言错误:', err);
    }
  };
  
  onMounted(fetchMessages);
  </script>
  
  <style scoped lang="scss">
  .message-board {
    background: #fafbfc;
    min-height: 100vh;
  }
  
  .main {
    padding: 48px 0 40px 0;
  }
  
  .container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 16px;
  }
  
  .message-intro {
    background: #fff;
    border-radius: 16px;
    padding: 36px;
    margin-bottom: 36px;
    text-align: center;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  }
  
  .welcome-message{
    font-size: 28px;
    font-weight: 700;
    color: #222;
    margin-bottom: 16px;
  }
  
  .intro-text {
    font-size: 16px;
    color: #888;
    line-height: 1.6;
  }
  
  .message-section,
  .comment-form-section {
    background: #fff;
    border-radius: 16px;
    padding: 28px;
    margin-bottom: 36px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  }
  
  .widget-title {
    font-size: 20px;
    font-weight: 600;
    color: #222;
    margin-bottom: 16px;
  }
  </style>